//
//  HDOperationWebViewController.m
//  HDOperationProject
//
//  Created by Mirko on 15/11/2.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "HDOperationWebViewController.h"
#import "HDOperationViewController.h"

@interface HDOperationWebViewController () <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *operationWebView;
@property (nonatomic, strong) NSString *loadWebUrl;

@property (nonatomic, weak) IBOutlet UIActivityIndicatorView *indicatorView;

@end

@implementation HDOperationWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = NO;

//    self.navigationItem.rightBarButtonItem = [self createBarButtonItem];
    [self.navigationItem setHidesBackButton:YES];
    
    [self parseParam];
    
    if (!self.loadWebUrl) {
        return;
    }
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.loadWebUrl]];
    self.operationWebView.delegate = self;
    self.operationWebView.scalesPageToFit = YES;
    [self.operationWebView loadRequest:request];
}

// 创建导航条按钮
- (UIBarButtonItem *)createBarButtonItem {
    
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 0, 40, 30);
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(handleNavRightButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc]initWithCustomView:doneButton];
}

-(void) dealloc
{
    [self.operationWebView stopLoading];
}

-(void) parseParam
{
    if (self.param && [self.param isKindOfClass:[NSDictionary class]]) {
        self.loadWebUrl = self.param[@"param"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)handleNavRightButtonEvent:(UIBarButtonItem *)button
{
//    [self.navigationController popViewControllerAnimated:NO];
    [self.operationWebView stopLoading];
    NSLog(@"Operation finish!");
    UIColor *defaultColor = ([UIApplication sharedApplication]).keyWindow.backgroundColor;
    ([UIApplication sharedApplication]).keyWindow.backgroundColor = [UIColor whiteColor];
    [UIView animateWithDuration:0.375 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            ([UIApplication sharedApplication]).keyWindow.backgroundColor = defaultColor;
            NSValue *value = self.param[@"Action"];
            HDOperationDidFinishBlock finishBlock = [value pointerValue];
            if (finishBlock) {
                [self.navigationController popViewControllerAnimated:NO];
                finishBlock();
            }
        }
    }];
}

#pragma mark UIWebView delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"响应: %@", request);
    
    [self.indicatorView startAnimating];

    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    [self.indicatorView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.indicatorView stopAnimating];

    if ([error code] == NSURLErrorCancelled) {
        return;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
